package com.kevin.v2.spring.formework.webmvc.servlet;

import com.sun.deploy.util.StringUtils;

import java.io.File;
import java.util.Locale;

/**
 * 将静态文件读取出来 渲染成一个动态文件
 */
public class KvViewResolver {

   private final String DEFAULT_SUFFIX=".html";

   //模板存放路径
   private File templateRootDir;

   //文件名称
   private String viewName;

   public KvViewResolver (String templatePath) {
      //拿到模板文件存放路径
      String templateRoot = this.getClass().getClassLoader().getResource(templatePath).getFile();
      templateRootDir = new File(templateRoot);
   }

   /**
    * 拿到要渲染的页面
    * @param viewName
    * @param o
    * @return
    * @throws Exception
    */
   public KvView resolveViewName (String viewName, Object o) throws Exception {
      this.viewName = viewName;
      if ("".equals(viewName)||null==viewName)return  null;

      viewName = viewName.endsWith(DEFAULT_SUFFIX) ? viewName : viewName + DEFAULT_SUFFIX;
      File templateFile = new File(templateRootDir.getPath()+"/"+viewName);

      return new KvView(templateFile);

   }

}
